package com.ruoyi.wms.controller;

import java.util.List;

import com.ruoyi.common.core.domain.bo.LoginAppUser;
import com.ruoyi.common.core.domain.bo.LoginUser;
import com.ruoyi.common.satoken.utils.LoginHelper;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.idempotent.annotation.RepeatSubmit;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.web.core.BaseController;
import com.ruoyi.common.mybatis.core.page.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.excel.utils.ExcelUtil;
import com.ruoyi.wms.domain.vo.MailboxMessageVo;
import com.ruoyi.wms.domain.bo.MailboxMessageBo;
import com.ruoyi.wms.service.MailboxMessageService;
import com.ruoyi.common.mybatis.core.page.TableDataInfo;

/**
 * 信件管理
 *
 * @author zcc
 * @date 2024-12-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wms/message")
public class MailboxMessageController extends BaseController {

    private final MailboxMessageService mailboxMessageService;

    /**
     * 查询信件管理列表
     */
    @SaCheckPermission("wms:message:list")
    @GetMapping("/list")
    public TableDataInfo<MailboxMessageVo> list(MailboxMessageBo bo, PageQuery pageQuery) {
        return mailboxMessageService.queryPageMessageList(bo, pageQuery);
    }

    /**
     * 导出信件管理列表
     */
    @SaCheckPermission("wms:message:export")
    @Log(title = "信件管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(MailboxMessageBo bo, HttpServletResponse response) {
        List<MailboxMessageVo> list = mailboxMessageService.queryList(bo);
        ExcelUtil.exportExcel(list, "信件管理", MailboxMessageVo.class, response);
    }

    /**
     * 获取信件管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("wms:message:query")
    @GetMapping("/{id}")
    public R<MailboxMessageVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(mailboxMessageService.queryById(id));
    }

    /**
     * 新增信件管理
     */
    @SaCheckPermission("wms:message:add")
    @Log(title = "信件管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody MailboxMessageBo bo) {
        mailboxMessageService.insertByBo(bo);
        return R.ok();
    }

    /**
     * 修改信件管理
     */
    @SaCheckPermission("wms:message:edit")
    @Log(title = "信件管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody MailboxMessageBo bo) {
        mailboxMessageService.updateByBo(bo);
        return R.ok();
    }


    @SaCheckPermission("wms:message:share")
    @Log(title = "信件分配", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/share")
    public R<Void> share(@Validated(EditGroup.class) @RequestBody MailboxMessageBo bo) {
        bo.setStatus("1");
        mailboxMessageService.updateByBo(bo);
        return R.ok();
    }

    @SaCheckPermission("wms:message:handle")
    @Log(title = "信件处理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/handle")
    public R<Void> handle(@Validated(EditGroup.class) @RequestBody MailboxMessageBo bo) {
        bo.setStatus("2");
        LoginUser loginUser = LoginHelper.getLoginUser();
        bo.setReply(loginUser.getUserId());
        bo.setReplyName(loginUser.getUsername());
        mailboxMessageService.updateByBo(bo);
        return R.ok();
    }
    /**
     * 删除信件管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("wms:message:remove")
    @Log(title = "信件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        mailboxMessageService.deleteByIds(List.of(ids));
        return R.ok();
    }
}
